package club.emergency.wechat.question.operate.consumer.controller;

import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.response.StatusCode;
import club.emergency.wechat.common.utils.JsonConvert;
import club.emergency.wechat.model.question.Question;
import club.emergency.wechat.model.question.QuestionDailyRecord;
import club.emergency.wechat.question.operate.consumer.service.QuestionOperateConsumer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.question.operate.consumer.controller
 * Date: 2018/11/28
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@Api("题目操作接口")
@RestController
@RequestMapping("/question-operate")
public class QuestionOperateController {

    private QuestionOperateConsumer questionOperateConsumer;

    public QuestionOperateController(QuestionOperateConsumer questionOperateConsumer) {
        this.questionOperateConsumer = questionOperateConsumer;
    }

    @ApiOperation("新增题目数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "题目内容", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "kind", value = "题目类型（0 单选，1 判断）", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "category", value = "题目类型", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "categoryId", value = "试题种类id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "categoryAlias", value = "试题种类关键字，从子字典表中获取", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "rightOption", value = "正确选项，如果有多选选项间需要以逗号分隔，例如：A,B,C", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "rightOptionContent", value = "正确选项内容", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "choiceOptionList", value = "选项json串", required = true, dataType = "string", paramType = "query")
    })
    @PostMapping("/save")
    public ResponseEntity save(
            @RequestParam("content") String content,
            @RequestParam("kind") Integer kind,
            @RequestParam("category") String category,
            @RequestParam("categoryId") Long categoryId,
            @RequestParam("categoryAlias") String categoryAlias,
            @RequestParam("rightOption") String rightOption,
            @RequestParam("rightOptionContent") String rightOptionContent,
            @RequestParam("choiceOptionList") String choiceOptionList
    ) {
        Question question = new Question();
        question.setContent(content);
        question.setKind(kind);
        question.setCategory(category);
        question.setCategoryId(categoryId);
        question.setCategoryAlias(categoryAlias);
        question.setRightOption(rightOption);
        question.setRightOptionContent(rightOptionContent);
        return new JsonConvert<ResponseEntity>().jsonDecode(questionOperateConsumer.save(question, choiceOptionList), ResponseEntity.class);
    }

    @ApiOperation("从excel中导入试题")
    @PostMapping("/saveFromExcel")
    public ResponseEntity saveFromExcel(
            MultipartFile file
    ) {
        String originalFilename = file.getOriginalFilename();
        if (Objects.isNull(originalFilename)) {
            return new ResponseEntity(StatusCode.DB_SAVE_ERROR, "获取文件名失败，请检查传入的文件方式是否正确！");
        }
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        InputStream inputStream;
        byte[] bytes = null;
        try {
            inputStream = file.getInputStream();
            bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new JsonConvert<ResponseEntity>().jsonDecode(questionOperateConsumer.saveFromExcel(suffix, bytes), ResponseEntity.class);
    }

    @ApiOperation("修改题目数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "content", value = "题目内容", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "kind", value = "题目类型（0 单选，1 判断）", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "category", value = "题目类型", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "categoryId", value = "试题种类id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "categoryAlias", value = "试题种类关键字，从子字典表中获取", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "rightOption", value = "正确选项，如果有多选选项间需要以逗号分隔，例如：A,B,C", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "rightOptionContent", value = "正确选项内容", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "choiceOptionList", value = "选项json串", required = true, dataType = "string", paramType = "query")
    })
    @PostMapping("/modify")
    public ResponseEntity modify(
            @RequestParam("id") Long id,
            @RequestParam("content") String content,
            @RequestParam("kind") Integer kind,
            @RequestParam("category") String category,
            @RequestParam("categoryId") Long categoryId,
            @RequestParam("categoryAlias") String categoryAlias,
            @RequestParam("rightOption") String rightOption,
            @RequestParam("rightOptionContent") String rightOptionContent,
            @RequestParam("choiceOptionList") String choiceOptionList
    ) {
        Question question = new Question();
        question.setId(id);
        question.setContent(content);
        question.setKind(kind);
        question.setCategory(category);
        question.setCategoryId(categoryId);
        question.setCategoryAlias(categoryAlias);
        question.setRightOption(rightOption);
        question.setRightOptionContent(rightOptionContent);
        return new JsonConvert<ResponseEntity>().jsonDecode(questionOperateConsumer.modify(question, choiceOptionList), ResponseEntity.class);
    }

    @ApiOperation("删除题目数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "choiceOptionIdList", value = "选项IdList", dataType = "Long", paramType = "query")
    })
    @PostMapping("/remove")
    public ResponseEntity remove(
            @RequestParam("id") Long id,
            @RequestParam(value = "choiceOptionIdList", required = false) String choiceOptionIdArr
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(questionOperateConsumer.remove(id, choiceOptionIdArr), ResponseEntity.class);
    }

    @ApiOperation("判断日常答题选项是否正确")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "题目内容", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "kind", value = "题目类型（0 单选，1 判断）", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "categoryId", value = "试题种类id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "categoryAlias", value = "试题种类关键字，从子字典表中获取", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "rightOption", value = "正确选项，如果有多选选项间需要以逗号分隔，例如：A,B,C", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "rightOptionContent", value = "正确选项内容", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "userOption", value = "用户选择选项", required = true, dataType = "string", paramType = "query"),
    })
    @PostMapping("/checkCorrectForDaily")
    public ResponseEntity checkCorrectForDaily(
            @RequestParam("questionId") Long questionId,
            @RequestParam("content") String content,
            @RequestParam("category") String category,
            @RequestParam("kind") Integer kind,
            @RequestParam("categoryId") Long categoryId,
            @RequestParam("categoryAlias") String categoryAlias,
            @RequestParam("rightOption") String rightOption,
            @RequestParam("rightOptionContent") String rightOptionContent,
            @RequestParam("userOption") String userOption
    ) {
        QuestionDailyRecord questionDailyRecord = new QuestionDailyRecord();
        questionDailyRecord.setQuestionId(questionId);
        questionDailyRecord.setCategory(category);
        questionDailyRecord.setContent(content);
        questionDailyRecord.setKind(kind);
        questionDailyRecord.setCategoryId(categoryId);
        questionDailyRecord.setCategoryAlias(categoryAlias);
        questionDailyRecord.setRightOption(rightOption);
        questionDailyRecord.setRightOptionContent(rightOptionContent);
        return new JsonConvert<ResponseEntity>().jsonDecode(questionOperateConsumer.checkCorrectForDaily(questionDailyRecord, userOption), ResponseEntity.class);
    }

}
